﻿/// <Summary> 
/// $Author: Nikhil Chinchwade 
/// This File Contains GraphCounterConfig class 

/// $Creation Date : 
/// </summary>
/// 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MindTree.PerfWorkbench.Azure.Monitor.Core;

namespace MindTree.PerfWorkbench.Azure.Monitor
{
    /// <summary>
    /// Interaction logic for GraphCounterConfig.xaml
    /// </summary>
    public partial class GraphCounterConfig : Window
    {
        public static List<PerfCounter> SelectedGraphCounters = new List<PerfCounter>();

        public static bool IsOK = false;

        public List<PerfCounter> counters = new List<PerfCounter>();

        GraphEntity graphentity = null;

        /// <summary>
        /// Default constructor
        /// </summary>
        public GraphCounterConfig()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Parameterised Constructor
        /// </summary>
        /// <param name="paramGraph">GraphControl object</param>
        public GraphCounterConfig(GraphControl paramGraph)
        {
            InitializeComponent();
            graphentity = paramGraph.View.GraphEnt;
        }

        /// <summary>
        /// Parameterised Constructor
        /// </summary>
        /// <param name="paramEntity">GraphEntity Object</param>
        public GraphCounterConfig(GraphEntity paramEntity)
        {
            InitializeComponent();
            graphentity = paramEntity;
        }

        /// <summary>
        /// Event handler for role selection changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RoleSelection_Changed(object sender, SelectionChangedEventArgs e)
        {
            if( lstBoxRoles.SelectedIndex != -1)
            {
                lstBoxInstances.ItemsSource = ((AzureRole)lstBoxRoles.SelectedItem).Instances;
            }
            //RoleInstanceSelection_Changed(this, null);
            counters.Clear();            
        }

        /// <summary>
        /// On_Load implementation
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void On_Load(object sender, RoutedEventArgs e)
        {
            lstBoxRoles.ItemsSource = AzureMonInstanceContext.AzureApp.AppDeployment[0].Roles;
            if (null != graphentity)
            {
                foreach (PerfCounter cntr in graphentity.GraphContext.Keys)
                {
                    lstBoxGraphCntrs.Items.Add(cntr);
                }
            }
            else
            {

            }
        }


        /// <summary>
        /// Event Handler for RoleInstanceSelection_Changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RoleInstanceSelection_Changed(object sender, SelectionChangedEventArgs e)
        {
            if (lstBoxRoles.SelectedItems.Count > 0)
            {
                counters.Clear();
                //List<PerfCounter> counters = new List<PerfCounter>();
                foreach (object o in lstBoxInstances.SelectedItems)
                {
                    lstBoxAvailableCntrs.ItemsSource = ((AzureRoleInstance)o).GetCounters();

                    foreach (PerfCounter cntr in ((AzureRoleInstance)o).GetCounters())
                    {
                        if (!counters.Contains(cntr))
                        {
                            cntr.MachineName = ((AzureRoleInstance)o).RoleInstanceId;
                            counters.Add(cntr);
                        }
                    }
                }   
 
                lstBoxAvailableCntrs.ItemsSource = counters;
            }
        }

        /// <summary>
        /// Add a new graph to the view
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddGraphCntr_Click(object sender, RoutedEventArgs e)
        {
            foreach( PerfCounter cntr in lstBoxAvailableCntrs.SelectedItems )
            {
                if (!lstBoxGraphCntrs.Items.Contains(cntr))
                {
                    lstBoxGraphCntrs.Items.Add(cntr);
                }
            }
        }

        /// <summary>
        /// Remove graph from view
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRemGraphCntr_Click(object sender, RoutedEventArgs e)
        {
            List<PerfCounter> cntrs = new List<PerfCounter>();
            
            foreach (PerfCounter cntr in lstBoxGraphCntrs.SelectedItems)
            {
                cntrs.Add(cntr);                
            }   
            foreach(PerfCounter cntr in cntrs)
            {
                lstBoxGraphCntrs.Items.Remove(cntr);               
            }
        }

        /// <summary>
        /// Save Graph Counters
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSaveGraphCntrs_Click(object sender, RoutedEventArgs e)
        {
            IsOK = true;
            if (null == lstBoxGraphCntrs.Items || lstBoxGraphCntrs.Items.Count <= 0)
            {
                IsOK = false;
                Close();
            }
            foreach (PerfCounter cntr in lstBoxGraphCntrs.Items)
            {
                SelectedGraphCounters.Add(cntr);
            }
            Close();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Cancel();
            Close();
        }

        /// <summary>
        /// Cancel implementation 
        /// </summary>
        private void Cancel()
        {
            IsOK = false;
            SelectedGraphCounters.Clear();
        }

        /// <summary>
        /// Window_Closed Event Handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Closed(object sender, EventArgs e)
        {
            if (!IsOK)
            {
                Cancel();
            }
        }
    }
}
